<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <!-- fixed 固定定位 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button 
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        class="search-btn"
        to="/search"
      >搜索</van-button>
    </van-nav-bar> 

    <!-- 判断列表 -->
    <!-- 通过 v-model 绑定当前激活标签对应的索引值，默认启用第一个标签。 -->
    <!-- 通过 animated 属性可以开启切换标签内容时的转场动画。 -->
    <!-- 通过 swipeable 属性可以开启滑动切换标签页 -->
    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <van-tab 
        v-for='channel in channels'
        :key="channel.id"
        :title="channel.name"
      >
        <!-- 文章列表 -->
        <ArticleList :channel="channel"></ArticleList>
      </van-tab>
      <!-- 此插槽仅用于占位，解决最后一个导航被遮挡的问题 -->
      <div slot="nav-right" class="placeholderr"></div>
      <div slot="nav-right" class="hamburger-btn" @click="isChennelEditShow = true">
        <i class="iconfont icon-gengduo"></i>
      </div>
    </van-tabs>

    <!-- 频道编辑弹出层 -->
    <van-popup
      v-model="isChennelEditShow"
      closeable
      close-icon-position="top-left"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <ChannelEdit 
        :my-channelss="channels"
        :active = 'active'
        @update-active="onUpdateActive"
      ></ChannelEdit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user'
import ArticleList from './components/article-list.vue'
import ChannelEdit from './components/channel-edit.vue';
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage';

export default {
  name: 'HomeIndex',
  components: {
    ArticleList,
    ChannelEdit
  },
  data() {
    return {
      active: 0,
      channels: [],  // 频道列表
      isChennelEditShow: false,  // 控制编辑频道弹出层的显示状态
    };
  },
  computed: {
    ...mapState(['user'])
  },
  created() {
    this.loadChannels()
  },
  methods: {
    async loadChannels() {
      try {
        // const { data: { data: { channels }} } = await getUserChannels()
        // this.channels = channels

        let channels = []
        
        if (this.user) {// 已登录，请求获取用户频道列表
          const { data } = await getUserChannels()
          channels = data.data.channels
        } else {
          // 未登录，判断是否有本地的频道列表数据
          const localChannels = getItem('TOUTIAO_CHANNELS')
          //      有，拿来使用
          if (localChannels) {
            channels = localChannels
          } else {
          //      没有，请求获取默认频道列表   
            const { data } = await getUserChannels()
            channels = data.data.channels
          }
        }
        this.channels = channels
      
      } catch (err) {
        this.$toast('获取频道列表数据失败')
      }

    },

    onUpdateActive(index,isChennelEditShow = true) {
      // 更新激活的频道项
      this.active = index
      // 关闭编辑频道弹层
      this.isChennelEditShow = isChennelEditShow
    }
  }
}
</script>



<style scoped lang="less">
  .home-container {
    padding-top: 174px;
    padding-bottom: 100px;
    .search-btn {
      width: 555px;
      height: 64px;
      background-color: #5babfb;
      border: none;
      font-size: 28px;
      .van-icon {
        font-size: 32px;
      }
    }

    /deep/ .channel-tabs{
      .van-tabs__wrap{   // 样式该如何设置，可以在调试面板先试一下，可以再拿过来
        height: 83px; 
        position: fixed;
        top: 92px;
        left: 0;
        right: 0;
        z-index: 1;     
      }
      .van-tab{
        min-width: 200px;
        border-right: 1px solid #edeff3;
        font-size: 30px;
        color:#777;
      }
      .van-tab--active{
        color:#333;
      }
      .van-tabs__nav{
        padding-bottom: 0;
      }
      .van-tabs__line{
        width: 31px!important;
        height: 6px;
        background-color: #3296fa;
	      border-radius: 3px;
        bottom:8px;
      }
      .hamburger-btn{
        position: fixed;
        right: 0;
        display: flex;
        justify-content: center;
        align-items: center;
        width: 66px;
        height: 82px;
        background-color: #fff;
        opacity: 0.902;
        .iconfont {
          font-size: 33px;
        }
        &:before{
          content:'';
          position: absolute;
          left: 0;
          // 这里设置 1px，在浏览器模拟器看不见，但在真实的手机页面上是可以看见的
          width: 1px;
          height: 100%;
          background-image: url(~@/assets/gradient-gray-line.png);
          background-size: contain;
        }
      }

      .placeholderr{
        width: 60px;
        height: 100%;
        // 此时会发现宽度不起作用，为0
        // 原因是它的父元素采用了flex布局，空间已经被 子元素van-tab平分完了
        // 此时可以设置这个 div 的 flex-shrink 为 0 。表示不参与父盒子的剩余空间平分计算
        flex-shrink: 0; 
      }

    }
  }
  </style>